﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using YanZhiwei.DotNet2.Utilities.Models;
namespace YanZhiwei.DotNet2.Utilities.WinForm
{
    /// <summary>
    /// MenuStrip创建 帮助类
    /// </summary>
    public class MenuStripBuildeHelper
    {
        #region 构造函数以及变量
        private Type type;
        private MenuStrip mStrip = null;
        private IList<MenuStripItem> mItem = null;
        object instce = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="instance">Type</param>
        /// <param name="menuStrip">MenuStrip</param>
        /// <param name="menuItems">MenuStrip数据集合</param>
        public MenuStripBuildeHelper(object instance, MenuStrip menuStrip, IList<MenuStripItem> menuItems)
        {
            type = instance.GetType();
            instce = instance;
            mStrip = menuStrip;
            mItem = menuItems;
        }
        #endregion 
        #region  动态创建MenuStrip
        /// <summary>
        /// 创建MenuStrip
        /// </summary>>
        public void BuilderMenuItem()
        {

            IList<MenuStripItem> _subMenuItems = WhereMenuItems(mItem, 0);
            if (_subMenuItems == null) return;
            foreach (MenuStripItem menuItem in _subMenuItems)
            {
                ToolStripMenuItem _subMenuItem = CreateSubMenuItem(mStrip, menuItem);
                BuilderMenuItem(_subMenuItem, menuItem.ID);
            }
        }
        private void BuilderMenuItem(ToolStripMenuItem _subMenuItem, int fatherID)
        {
            IList<MenuStripItem> _menuItems = WhereMenuItems(mItem, fatherID);
            if (_menuItems == null) return;
            foreach (MenuStripItem menuItem in _menuItems)
            {
                ToolStripMenuItem _menuItem = CreateMenuItem(menuItem, _subMenuItem);
                BuilderMenuItem(_menuItem, menuItem.ID);
            }
        }
        private ToolStripMenuItem CreateMenuItem(MenuStripItem menuItem, ToolStripMenuItem _subMenuItem)
        {
            ToolStripMenuItem _menuItem = new ToolStripMenuItem();
            _menuItem.Name = menuItem.MENU_NAME;
            _menuItem.Text = menuItem.MENU_TEXT;
            _menuItem.Tag = menuItem.ACTION_NAME;
            _menuItem.Click += MenuItem_Click;
            _subMenuItem.DropDownItems.Add(_menuItem);
            return _menuItem;
        }
        private void MenuItem_Click(object sender, EventArgs e)
        {
            string _actionName = ((ToolStripMenuItem)sender).Tag.ToString();
            if (!string.IsNullOrEmpty(_actionName))
            {
                try
                {
                    MethodInfo Methodinfo = type.GetMethod(_actionName);
                    if (Methodinfo == null || instce == null) return;
                    Methodinfo.Invoke(instce, null);
                }
                catch (Exception)
                {
                }
            }
        }
        private ToolStripMenuItem CreateSubMenuItem(MenuStrip menu, MenuStripItem menuItem)
        {
            ToolStripMenuItem _menuItem = new ToolStripMenuItem();
            _menuItem.Name = menuItem.MENU_NAME;
            _menuItem.Text = menuItem.MENU_TEXT;
            _menuItem.Tag = menuItem.ACTION_NAME;
            _menuItem.Click += MenuItem_Click;
            menu.Items.Add(_menuItem);
            return _menuItem;
        }
        private IList<MenuStripItem> WhereMenuItems(IList<MenuStripItem> menuItems, int fatherID)
        {
            IList<MenuStripItem> _whereMenuItems = new List<MenuStripItem>();
            foreach (MenuStripItem menuItem in menuItems)
            {
                if (menuItem.FATHER_ID == fatherID)
                    _whereMenuItems.Add(menuItem);
            }
            return _whereMenuItems;
        }
        #endregion 
    }
}
